﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

namespace TrinityAPI.Base.Tools
{
    public class DecimalHelper
    {
        public static Decimal Add(object d1, object d2, params object[] any)
        {
            decimal result = ToDecimal(d1) + ToDecimal(d2);
            foreach (var item in any)
                result += ToDecimal(item);
            return result;
        }

        public static Decimal Subtract(object d1, object d2, params object[] any)
        {
            decimal result = ToDecimal(d1) - ToDecimal(d2);
            foreach (var item in any)
                result -= ToDecimal(item);
            return result;
        }

        public static Decimal Multiply(object d1, object d2, params object[] any)
        {
            decimal result = ToDecimal(d1) * ToDecimal(d2);
            foreach (var item in any)
                result *= ToDecimal(item);
            return result;
        }

        public static Decimal Divide(object d1, object d2, params object[] any)
        {
            decimal result = ToDecimal(d1) / ToDecimal(d2);
            foreach (var item in any)
                result /= ToDecimal(item);
            return result;
        }

        public static Decimal Negate(object d)
        {
            return ToDecimal(d) * -1;
        }

        public static Decimal Ceiling(object d)
        {
            return Decimal.Ceiling(ToDecimal(d));
        }

        public static Decimal Floor(object d)
        {
            return Decimal.Floor(ToDecimal(d));
        }

        public static int ToInt32(object d)
        {
            return Decimal.ToInt32(ToDecimal(d));
        }

        public static Decimal ToDecimal(object value)
        {
            if (value == null)
                return 0m;
            else if (value is decimal)
                return (decimal)value;
            else if (value is float)
                return new Decimal((float)value);
            else if (value is double)
                return new Decimal((double)value);
            else if (value is byte)
                return new Decimal((byte)value);
            else if (value is short)
                return new Decimal((short)value);
            else if (value is ushort)
                return new Decimal((ushort)value);
            else if (value is int)
                return new Decimal((int)value);
            else if (value is uint)
                return new Decimal((uint)value);
            else if (value is long)
                return new Decimal((long)value);
            else if (value is ulong)
                return new Decimal((ulong)value);
            else if (value is string)
            {
                decimal val = 0;
                decimal.TryParse(value.ToString(), out val);
                return val;
            }
            else
                return 0m;
        }
    }
}